import userStore from '@/stores/store';
import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'startpage',
      redirect:"/login",
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login.vue'),
    },
    {
      path: '/signup',
      name: 'signup',
      component: () => import('@/views/Signup.vue')
    },
    {
      path:'/homepage',
      name:'homepage',
      component: () => import('@/views/WelcomeHome.vue')
    },
    {
      path:'/managesys',
      name:'managesys',
      component: () => import('@/views/LayoutBase.vue'),
      children:[
        {
          path:'clublist',
          name:'clublist',
          component: () => import('@/components/ClubList.vue')
        },
        {
          path:'clubview',
          name:'clubview',
          component: () => import('@/components/ClubView.vue'),
        },
        {
          path:'myclubjoin',
          name:'myclubjoin',
          component: () => import('@/components/MyClubJoin.vue')
        },
        {
          path:'myclubmanage',
          name:'myclubmanage',
          component: () => import('@/components/MyClubManage.vue'),
          beforeEnter:() =>{
            const flag = routeLogout(2);
            if(!flag){
              return { name:'login' }
            }
            else{ return; }
          }
        },
        {
          path:'person',
          name:'person',
          component: () => import('@/components/SelfInfo.vue')
        },

        {
          path:'noticepage',
          name:'noticepage',
          component: () => import('@/components/NoticePage.vue')
        },
        {
          path:'myworks',
          name:'myworks',
          component: () => import('@/components/MyWorks.vue')
        },
        {
          path:'transapproval',
          name:'transapproval',
          component: () => import('@/components/TransApproval.vue'),
          beforeEnter:() =>{
            const flag = routeLogout(2);
            if(!flag){
              return { name:'login' }
            }
            else{ return; }
          }
        },
        {
          path:'clubadd',
          name:'clubadd',
          component: () => import('@/components/ClubAdd.vue')
        },
        {
          path:'clubedit',
          name:'clubedit',
          component: () => import('@/components/ClubEdit.vue'),
          beforeEnter:() =>{
            const flag = routeLogout(1);
            if(!flag){
              return { name:'login' }
            }
            else{ return; }
          }
        },
        {
          path:'memberinfo',
          name:'memberinfo',
          component: () => import('@/components/MemberInfo.vue'),
          beforeEnter:() =>{
            const flag = routeLogout(2);
            if(!flag){
              return { name:'login' }
            }
            else{ return; }
          }
        },
        {
          path:'recommend',
          name:'recommend',
          component: () => import('@/components/RecommendWork.vue'),
          beforeEnter:() =>{
            const flag = routeLogout(1);
            if(!flag){
              return { name:'login' }
            }
            else{ return; }
          }
        }
      ]
    }
  ],
})

export const routeLogout = (requiredPermission) =>{
  const store = userStore()
  const nowPermission = store.getPermission;
  if(nowPermission >= 0 && nowPermission <= requiredPermission){
    return true;
  }
  else{
    store.logout();
    return false;
  }
}
export default router;
